//index.js
const app = getApp();
let that = null;
import store from "../../factory"
import * as Request from '../../request';
import * as mouble from '../../until';
Page({
  data: {
    companyList: [],
    orderList: [],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    active: 0,
    showModal: false,
    dialogStatus: "customer",
    confrimText: "确定",
    type: 1,
    statisticsInfo: {},
    saleInfo: {},
  },

  onLoad: function () {
    that = this;
  },
  onShow() {
    if (store.get("token")) {
      let userType = store.get("userType");
      console.log("userType", userType)
      that.setData({
        type: userType,
        active: 1
      })
      that.initData();
    }
  },
  logout() {
    mouble.wxErrorTips("是否确认退出登录?", true, function () {
      store.clear();
      wx.navigateTo({
        url: "../autoLogin/autoLogin"
      })
    })
  },
  initData() {
    let active = that.data.active;
    let type = that.data.type;
    if (active == 1) {
      if (store.get("userInfo")) {
        that.setData({
          saleInfo: store.get("userInfo")
        })
        that.indexStatistics();
        that.getBannerInfo()
        return false
      }
      that.getUserInfo(type)
    }
    if (active == 2) {
      that.getOrderList(type)
    }
    if (active == 3) {
    }
  },
  getBannerInfo() {
    Request.banner().then(res => {
      console.log("banner", res)
      if (res.code == 200) {
        if (res.data) {
          that.setData({
            companyList: res.data
          })
        }
      } else {
        mouble.wxErrorTips(res.message)
      }
    })
  },
  getOrderList(type) {
    if (type == 1) {
      that.getSalesOrderList()
    }
    if (type == 2) {
      that.getCustomerOrderList()
    }
  },
  indexStatistics() {
    Request.indexStatistics().then(res => {
      if (res.code == 200) {
        that.setData({
          statisticsInfo: res.data
        })
      }
    })
  },
  getCustomerOrderList() {
    let formData = {
      "customerId": 0,
      "from": "string",
      "groupId": 0,
      "orderType": 0,
      "page": 1,
      "quoteDate": "string",
      "salesId": 0,
      "size": 0,
      "sortType": 0,
      "status": 0,
      "to": "string"
    };
    getCustomerOrderList().then(res => {
      console.log(res);
    })
  },
  getSalesOrderList() {
    let formData = {
      "customerId": 0,
      "from": "string",
      "groupId": 0,
      "orderType": 0,
      "page": 0,
      "quoteDate": "string",
      "salesId": 0,
      "size": 0,
      "sortType": 0,
      "status": 0,
      "to": "string"
    };
    Request.getSalesOrderList({
      page: 1
    }).then(res => {
      if (res.code == 200) {
        console.log(res)
        let req = res.data.data;
        req.map(item => {
          if (item.orderType == 1) {
            item.orderTypeName = "微信转售单"
          }
          if (item.orderType == 2) {
            item.orderTypeName = "委托单"
          }
          item.statusName = that.statusToStr(item.status)
        })
        that.setData({
          orderList: req
        })
      }
      console.log(res)
    })
  },
  statusToStr(status) {
    switch (status + "") {
      case "0":
        return "未确认";
      case "1":
        return "待支付";
      case "2":
        return "已分配";
      case "3":
        return "已取消";
      default:
        return "未知";
    }
  },
  getUserInfo(userType) {
    if (userType == 1) {
      Request.getContactsInfo().then(res => {
        if (res.code == 200) {
          if (res.data) {
            that.setData({
              saleInfo: res.data,
              inviteCode: res.data.invite
            })
            store.set("userInfo", res.data)
          } else {
            wx.showToast({
              title: "未获取到用户信息",
              icon: "none"
            })
          }
        } else {
          mouble.wxErrorTips(res.message)
        }
        that.indexStatistics();
        // that.companyStatistics()
      })
    } else {
      Request.getUserInfo().then(res => {
        console.log(res)
      })
    }
  },
  confirm() {
    this.setData({
      showModal: false
    })
  },
  start() {
    wx.navigateTo({
      url: "../autoLogin/autoLogin"
    })
  },
  goOrderList() {
    that.setData({
      active: 2
    })
  },
  call(e) {
    const data = e.currentTarget.dataset.dialogstatus;
    this.setData({
      dialogStatus: data,
      showModal: true
    })
  },
  hideModal() {
    this.setData({
      showModal: false
    })
  },
  change(e) {
    const data = e.currentTarget.dataset.active;
    that.setData({
      active: data
    })
    console.log("active", data)
    that.initData()
  },
  routerGo(e) {
    app.routerGo(e)
  },
  goCompanyDetail(e) {
    let data = e.currentTarget.dataset;
    store.set("companyId", data.id)
    store.set("groupName", data.name)
    wx.navigateTo({
      url: `../${data.page}/${data.page}`
    })
  },
  goDetail(e) {
    let data = e.currentTarget.dataset;
    store.set("orderId", data.id)
    wx.navigateTo({
      url: `../${data.page}/${data.page}`
    })
  },

})
